<template>
  <input type="text" v-model="name" />
  <p>hello {{ name }}</p>
  <button @click="name = 'world'">change</button>
  <button @click="name = 'Vue'">reset</button>
  <button @click="name = 'vue3'">vue3</button>
  <button @click="name = 'react'">react</button>
</template>

<script>
import { customRef } from "vue";
export default {
  name: "Demo",
  setup() {
    const myRef = (value, delay) => {
      let timer;
      return customRef((track, trigger) => {
        return {
          get() {
            track();
            return value;
          },
          set(newValue) {
            clearTimeout(timer);
            timer = setTimeout(() => {
              value = newValue;
              trigger();
            }, delay);
          },
        };
      });
    };

    const name = myRef("Vue", 1000);

    return {
      name,
    };

    return {};
  },
};
</script>
